//
//  HWColorExtension.swift
//  PlayWithProject
//
//  Created by 王昌健 on 2021/7/9.
//

import Foundation
import UIKit

extension UIColor{
    
    @objc class func colorWithHexString(hexString:String) -> UIColor {
        return colorWithAlphaHexString(hexString: hexString, alpha: 1)
    }
    
    @objc class func colorWithAlphaHexString(hexString:String, alpha:CGFloat) -> UIColor {
        var colorString = hexString.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines).uppercased()
        if colorString.count < 6 {
            return UIColor.clear
        }
        
        if hexString.hasPrefix("0x") {
            colorString = (colorString as NSString).substring(from: 2)
        }
        
        if hexString.hasPrefix("#") {
            colorString = (colorString as NSString).substring(from: 1)
        }
        
        if colorString.count != 6 {
            return UIColor.clear
        }
        
        var range = NSRange()
        range.location = 0
        range.length = 2
        
        let  rString = (colorString as NSString).substring(with: range)
        range.location = 2
        let  gString = (colorString as NSString).substring(with: range)
        range.location = 4
        let  bString = (colorString as NSString).substring(with: range)
        
        var r:UInt64 = 0 ,g:UInt64 = 0 ,b:UInt64 = 0
        
        Scanner(string: rString).scanHexInt64(&r)
        Scanner(string: gString).scanHexInt64(&g)
        Scanner(string: bString).scanHexInt64(&b)
        
        return UIColor.init(red: CGFloat(r)/255.0, green: CGFloat(g)/255.0, blue: CGFloat(b)/255.0, alpha: alpha)
    }
    
    class func gradientColor(view:UIView,colors:Array<CGColor>,start:CGPoint,end:CGPoint) -> Void {
        
        //        let gradientColors = [UIColor.colorWithHexString(hexString: "#F7E8FF").cgColor,
        //                              UIColor.colorWithHexString(hexString: "#FFFEFF").cgColor,
        
        //        ]
        
        //创建CAGradientLayer对象并设置参数
        let gradientLayer = CAGradientLayer()
        gradientLayer.colors = colors
        //        CGPoint(x: 0, y: 0) CGPoint(x: 1, y: 0)
        //设置渲染的起始结束位置（横向渐变）
        gradientLayer.startPoint = start
        gradientLayer.endPoint = end
        
        //设置其CAGradientLayer对象的frame，并插入view的layer
        gradientLayer.frame = view.bounds
        view.layer.insertSublayer(gradientLayer, at: 0)
    }
    
    
    
    
    class func gradientLabel(labelBg:UIView,label:UILabel,colors:Array<CGColor>,start:CGPoint,end:CGPoint) -> Void {
        //        let labelBg = UIView()
        //        labelBg.frame = label.frame
        //创建CAGradientLayer对象
        let gradientLayer = CAGradientLayer()
        
        //设置初始渐变色
        gradientLayer.colors = colors
        
        //设置每种颜色初始所在的位置
        //        gradientLayer.locations = [0, 0, 0.25]
        
        //设置渲染的起始结束位置（横向渐变）
        gradientLayer.startPoint = start
        gradientLayer.endPoint = end
        
        //设置其CAGradientLayer对象的frame，并插入view的layer
        gradientLayer.frame = label.bounds
        
        //将渐变层添加到文本标签背景上
        labelBg.layer.insertSublayer(gradientLayer, at: 0)
        
        //        //添加渐变动画（让白色光泽从左向右移动）
        //        let gradientAnimation = CABasicAnimation(keyPath: "locations")
        //        gradientAnimation.fromValue = [0, 0, 0.2]
        //        gradientAnimation.toValue = [0.8, 1, 1]
        //        gradientAnimation.duration = 12.5
        //        //动画一致重复执行
        //        gradientAnimation.repeatCount = HUGE
        //        self.gradientLayer.add(gradientAnimation, forKey: nil)
        
        //设置遮罩，让渐变层透过文字显示出来
        gradientLayer.mask = label.layer
    }
    
    class func createImageWithColor(color: UIColor) -> UIImage
    {
        let rect = CGRect(x: 0, y: 0, width: 1, height: 1)
        UIGraphicsBeginImageContextWithOptions(rect.size, false, 0)
        let context = UIGraphicsGetCurrentContext()
        context?.setFillColor(color.cgColor)
        context?.fill(rect)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        
        return image!
    }
}
